import { baseUrl } from '../../../../utils/config'
const { get } = require('../../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chatContentHeight: 100,
    popHeight: 70,
    defaultPopHeight: 70,
    baseUrl: '',
    show: false,
    duration: 300,
    position: 'bottom',
    title: "",
    replyMsg: "",
    headPicPath: "",
    consultData: [],
    consultId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad({ id }) {
    this.freshData(id);

    const screenHeight = wx.getWindowInfo().screenHeight;

    if (screenHeight > 400) {
      this.setData({
        chatContentHeight: screenHeight - 400
      })
    }

  },

  freshData(id) {
    get('consult/chatData', {
      "consultId": id
    }, ({ chatData, title, headPicPath }) => {
      chatData.forEach(item => {
        item.createTimeStr = item.createTime.substring(0, 10)
      })
      this.setData({
        baseUrl: baseUrl,
        consultData: chatData,
        title: title,
        headPicPath: headPicPath,
        consultId: id
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTouchStart({ currentTarget: { id } }) {
    let flag = this.data.consultData[id].showReply;

    if (flag) {
      this.setData({
        show: true
      })
    }
  },
  confimAddReply() {
    const params = {
      content: this.data.replyMsg,
      'consult.id': this.data.consultId
    }

    this.setData({
      popHeight: this.data.defaultPopHeight
    })

    if (this.data.replyMsg == '') {
      return;
    }

    get('consultchat/save', params, () => {
      this.freshData(this.data.consultId);
      this.setData({
        replyMsg: ''
      })
    })
  },
  onHeightChange({ detail: { height } }) {
    this.setData({
      popHeight: this.data.defaultPopHeight + height
    })
  },
  onclickOverlay() {
    this.setData({
      popHeight: this.data.defaultPopHeight
    })
  },
  onStartScroll() {
    if (this.data.popHeight > this.data.defaultPopHeight) {
      this.setData({
        popHeight: this.data.defaultPopHeight
      })
    }

  }

})